package com.xscj.dao.impl;

import com.xscj.dao.MessageDao;
import com.xscj.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class JdbcMessageDaoImpl implements MessageDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int getAllMessageCount() {
        String sqlStr = "SELECT count(*) From sp_message;";
        return jdbcTemplate.queryForInt(sqlStr);
    }

    @Override
    public int getAllMessageCountByUserType(int userType, String userId) {
        String sqlStr = "SELECT count(*) From sp_message where user_type = ? and user_id = ?;";
        Object[] args = {userType, userId};
        return jdbcTemplate.queryForInt(sqlStr, args);
    }

    private List<Message> getMessageList(String sqlStr, Object[] args) {
        final List<Message> messageList = new ArrayList<Message>();
        jdbcTemplate.query(sqlStr, args, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                Message message = new Message();
                message.setId(rs.getLong("id"));
                message.setUserId(rs.getString("user_id"));
                message.setUserName(rs.getString("user_name"));
                message.setUserType(rs.getInt("user_type"));
                message.setMessageTitle(rs.getString("message_title"));
                message.setMessageContent(rs.getString("message_content"));
                message.setMessageTime(rs.getDate("message_time"));
                message.setStatus(rs.getInt("status"));
                message.setReplyContent(rs.getString("reply_content"));
                message.setReplyTime(rs.getDate("reply_time"));
                messageList.add(message);
            }
        });
        return messageList;
    }

    @Override
    public List<Message> queryByPage(int pageSize, int pageNow) {
        String sqlStr = "SELECT * FROM sp_message order by id desc limit ?, ?;";
        Object[] args = {pageNow * pageSize - pageSize, pageSize};
        return getMessageList(sqlStr, args);
    }

    @Override
    public List<Message> queryByPageByUserType(int pageSize, int pageNow, int userType, String userId) {
        String sqlStr = "SELECT * FROM sp_message where user_type = ? and user_id = ? order by id desc limit ?, ?;";
        Object[] args = {userType, userId, pageNow * pageSize - pageSize, pageSize};
        return getMessageList(sqlStr, args);
    }

    @Override
    public int insertMessage(Message message) {
        String sqlStr = "INSERT INTO sp_message(user_id, user_type, user_name, message_title, message_content, " +
                "message_time, status) VALUES(?,?,?,?,?,?,?);";
        Object[] args = {message.getUserId(), message.getUserType(), message.getUserName(), message.getMessageTitle(),
                message.getMessageContent(), message.getMessageTime(), message.getStatus()};
        return jdbcTemplate.update(sqlStr, args);
    }

    @Override
    public int updateMessage(Message message) {
        String sqlString = "UPDATE sp_message SET reply_content = ?, reply_time = ?, status = 1 " +
                "WHERE id = ?;";
        Object[] argsObjects = {message.getReplyContent(), message.getReplyTime(), message.getId()};
        return jdbcTemplate.update(sqlString, argsObjects);
    }
}
